<template>
  <uni-nav-bar title="订单详情" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package center" v-if="orderAll.status != 1">
    未支付，请在
    <uni-section type="line" padding>
      <uni-countdown
        :second="orderAll.cha"
        :show-day="false"
        color="#FF0000"
        splitorColor="#FF0000"
        :font-size="16"
        @timeup="timeup"
      />
    </uni-section>
    内完成支付
  </view>
  <view class="package" style="padding:20rpx 0px">
    <view v-for="(item, index) in orderAll.goods_list" :key="index">
      <view class="packageLsit" @click="navTo('shopp/Shopkeeper?id=' + item.store_id)">
        <image src="../../static/groupBenefits/shop.png" mode=""></image>
        <text>{{ item.store_name }}</text>
        <uni-icons type="forward" size="19" color="#666"></uni-icons>
      </view>
      <ShopList :type="'num'" :shoppAll="item"></ShopList>
    </view>
    <Shoppay :payFor="orderAll" :state="'shopp'"></Shoppay>
  </view>
  <PayTime :payFor="orderAll"></PayTime>
  <ShopBot
    :title="title"
    @submit="submit"
    :empty="true"
    :total="orderAll.money"
    :Discount="orderAll.yh_money"
    v-if="title"
  ></ShopBot>
</template>

<script setup lang="ts">
import { navBack, navTo, redirectTo } from '@/utils/navigator';
import { chooseWXPay, defaultFail } from '@/utils/wechat.jssdk';
import { errorToast, successToast,showLoading } from '@/utils/prompt';
import ShopList from '@c/groupBenefits/shopGrid.vue';
import Shoppay from '@c/groupBenefits/pay.vue';
import PayTime from '@c/groupBenefits/payTime.vue';
import ShopBot from '@c/groupBenefits/shopBot.vue';
import { ref } from 'vue';
import { orderDetail } from '@/gql/shop';
import { toPublish } from '@mqtt';
import { onLoad } from '@dcloudio/uni-app';
import { getDriverID } from '@/stores/driverID';
/**
 * 商品订单详情
 * @param user_id  用户ID
 * @param orderID  订单ID
 * @param orderAll  订单全部信息
 * @param shopText  店铺名
 * @param empty  提交订单按钮状态
 * @param title  提交订单文字提示
 */
const user_id = getDriverID();
const orderID = ref();
const orderAll = ref({});
const shopText = ref('');

const empty = ref(true);

const title = ref('');
const repeat = ref(true)//放重复点击
onLoad((pearm) => {
  orderID.value = Number(pearm.id);
  init();
});
/**
 * 初始化获取订单信息
 * @param order_id  订单ID
 */
function init() {
  const payload = {
    query: orderDetail,
    variables: {
      order_id: orderID.value,
    },
  };
  toPublish('ql/store/getOrderGoodsInfo', payload, (obj: any) => {
    const { order_goods_info } = obj.data;
    orderAll.value = order_goods_info;
    title.value = order_goods_info.status == '1' ? '再来一单' : order_goods_info.status == '0' ? '去支付' : '';
    shopText.value = order_goods_info.goods_list[0].store_name;
    if (order_goods_info.status == '0' && order_goods_info.cha == '0') {
      empty.value = false;
    }
  });
}
/**
 * 提交购买
 * @param order_id  订单ID
 * @param user_id  用户ID
 */
function submit() {
  if(!repeat.value){
    return
  }
  repeat.value=false
  if (orderAll.value.status == '1') {
    const payload = {
      user_id,
      order_id: Number(orderAll.value.order_id),
    };
    toPublish('store/addShopOrderByOrder', payload, (obj: any) => {
      const { code, msg, data } = obj;
      if (code === 1) {
        repeat.value=true
        successToast(msg);
        uni.navigateTo({
          url: '/pages/shopp/sureOrder?id=' + data.order_id + '&shopName=' + shopText.value,
        });
      } else {
        repeat.value=true
        errorToast(msg);
      }
    });
  } else if (orderAll.value.status == '0') {
    const payload = {
      order_id: Number(orderAll.value.order_id),
      method: 'mp',
    };
    toPublish('store/payWechatByOrderGoods', payload, (obj: any) => {
      const { code, msg, data } = obj;
      if (code === 1) {
        repeat.value=true
        successToast(msg);
        weach(data);
      } else {
        repeat.value=true
        errorToast(msg);
      }
    });
  }
}
function weach(num) {
  const payload = {
    timeStamp: num.timeStamp,
    nonceStr: num.nonceStr,
    package: num.package,
    signType: num.signType,
    paySign: num.paySign,
  };
  function success(res: any) {
    successToast('支付成功');
    setTimeout(() => {
      redirectTo('shopp/success');
    }, 500);
  }
  function fail() {
    errorToast('支付失败 请稍后重试');
    return;
  }
  chooseWXPay(payload, success, defaultFail('chooseWXPay', fail));
}
/**
 * @param {Object} e 倒计时结束触发
 */
function timeup(e) {
  if (orderID.value) {
    init();
  }
}
</script>

<style scoped lang="less">
.packageLsit {
  width: 100%;
  display: flex;
  align-items: center;
  padding: 0px 20rpx;
  box-sizing: border-box;
  image {
    width: 50rpx;
    height: 50rpx;
  }
  text {
    font-size: 30rpx;
    font-weight: 400;
    color: #181818;
    margin: 0px 10rpx;
  }
}
</style>
